package com.Servlet;

import com.Bean.Message;
import com.Bean.Ticket;
import com.Util.DB;
import com.Util.MessDao;
import com.Util.TkDao;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/addmessage")
public class addmessage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username= req.getParameter("username");
        String tkid=req.getParameter("tkid");
        String content=req.getParameter("content");

        if(DB.isDebug())
        {
            System.out.println(username);
            System.out.println(tkid);
        }
        Message message=new Message();
        message.setUsername(username);
        message.setContent(content);
        message.setTkid(tkid);
        Ticket tk = TkDao.selectTkbyId(tkid);
        tk.setStatus("open");
        TkDao.updateTk(tk);

        if (DB.isDebug()) {
            JSONObject jb = JSONObject.fromObject(message);
            String tojson = jb.toString();
            System.out.println(tojson);
        }
        MessDao.updateMess(message);
        //req.getSession().setAttribute("tkid",tkid);
        resp.sendRedirect("./serve/message.jsp?tkid="+tkid+"&u="+username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
